/*
 * Copyright (c) 2011-2024, Cpjit (cpjit@qq.com).
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.cpjit.data.web.config;

import com.cpjit.data.web.MyBatisPlusPageHandlerMethodArgumentResolver;
import com.cpjit.data.web.RequestBodyPageArgumentResolver;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.util.Lazy;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.List;

/**
 * @author yonghuan[cpjit@qq.com]
 */
@ConditionalOnClass({MyBatisPlusPageHandlerMethodArgumentResolver.class, WebMvcConfigurer.class})
@Configuration(proxyBeanMethods = false)
public class DataMyBatisWebConfiguration implements WebMvcConfigurer {

    private final Lazy<MyBatisPlusPageHandlerMethodArgumentResolver> pageableResolver;
    private final Lazy<MyBatisPlusPageHandlerMethodArgumentResolverCustomizer> pageableResolverCustomizer;

    public DataMyBatisWebConfiguration(ApplicationContext context) {
        this.pageableResolver = Lazy.of( //
                () -> context.getBean("myBatisPlusPageableResolver", MyBatisPlusPageHandlerMethodArgumentResolver.class));
        this.pageableResolverCustomizer = Lazy.of( //
                () -> context.getBeanProvider(MyBatisPlusPageHandlerMethodArgumentResolverCustomizer.class).getIfAvailable());
    }

    @Override
    public void addArgumentResolvers(List<HandlerMethodArgumentResolver> resolvers) {
        resolvers.add(pageableResolver.get());
    }

    @Bean
    public MyBatisPlusPageHandlerMethodArgumentResolver myBatisPlusPageableResolver() {
        MyBatisPlusPageHandlerMethodArgumentResolver pageableResolver = new MyBatisPlusPageHandlerMethodArgumentResolver();
        customizePageableResolver(pageableResolver, null);
        return pageableResolver;
    }

    @Bean
    public RequestBodyPageArgumentResolver requestBodyPageArgumentResolver() {
        RequestBodyPageArgumentResolver bodyPageArgumentResolver = new RequestBodyPageArgumentResolver();
        customizePageableResolver(null, bodyPageArgumentResolver);
        return bodyPageArgumentResolver;
    }

    protected void customizePageableResolver(MyBatisPlusPageHandlerMethodArgumentResolver pageableResolver, RequestBodyPageArgumentResolver bodyArgumentResolver) {
        pageableResolverCustomizer.getOptional().ifPresent(c -> c.customize(pageableResolver, bodyArgumentResolver));
    }
}
